﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using pos.excption;
using System.Collections;
using Spring.Data.NHibernate.Support;
using Spring.Data.NHibernate;
using NHibernate;
using NHibernate.Criterion;

namespace pos.entity
{
    class SaleSettleAll : IHibernateCallback
    {
         private idea.tools.IProgress step;
         private DetachedCriteria dc;
         public SaleSettleAll(idea.tools.IProgress step,DetachedCriteria dc)
         {
             this.dc = dc;
             this.step = step;
         }
         public object DoInHibernate(ISession session)
        {

            if (step == null)
                return null;

            t_sale sale = new t_sale();
            ISQLQuery sql = session.CreateSQLQuery(
            string.Format("update t_salereport set unsettleAllAmount=0,unsettleAllMoney=0,unsettleAmount=0,unsettleMoney=0 where saleDate='{0}'", DateTime.Now.Date.ToString()));
            sql.ExecuteUpdate();
            session.Flush();
             // db.ExeSql(string.Format("update t_salereport set unsettleAllAmount=0,unsettleAllMoney=0,unsettleAmount=0,unsettleMoney=0 where saleDate='{0}'", DateTime.Now.Date.ToString()));

                SettleTool tool = new SettleTool();
                //string.Format("where saletime>'{0}' and saletime<'{1}'",txtBeginTime.Text,txtEndTime.Text)
                //select   DATE_FORMAT(saleTime,"%Y-%m-%d")  as t,commodityID, sum(saleAmount),sum(saleAmount*salePrice) from t_hsale group by commodityID,t 
      //          DetachedCriteria dc = sale.DC;
                dc.SetProjection(Projections.ProjectionList()
           //      .Add(Projections.Alias(Projections.SqlGroupProjection("DATE_FORMAT(saleTime,\"%Y-%m-%d\") as date ", "date", new String[] { "date" }, new NHibernate.Type.IType[] { NHibernateUtil.Date }), "row1"))
                 .Add(Projections.Alias(Projections.SqlGroupProjection("commodityID as c ", "c", new String[] { "c" }, new NHibernate.Type.IType[] { NHibernateUtil.String }), "row2"))
                  
                   .Add(Projections.SqlProjection(" sum(saleAmount) as saleAmount", new String[] { "saleAmount" }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Double }))
                     .Add(Projections.SqlProjection(" sum(saleAmount*salePrice) as saleMoney", new String[] { "saleMoney" }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Double }))
                
                    );

             //   IList list = sale.List(dc.Add(Restrictions.Eq("saleType", 0)).AddOrder(Order.Asc("seqID")));
                IList list = sale.List(dc);
                //"where saleType=0", "order by seqID");
                //      DataTable t = set.Tables[0];
                long i = 0;
                step.Init(list.Count);
                
                foreach (Object[] r in list)
                {
                    //sale.Init(r);
                    sale.commodityID = r[0].ToString();
                    sale.saleTime = DateTime.Now;
                    sale.saleAmount = Convert.ToDouble(r[1]);
                    sale.salePrice = Convert.ToDouble(r[2]) / Convert.ToDouble(r[1]);
                    sale.settleState = t_sale.SETTLENO;
                    SetttleDataItem item = tool.Setttle(sale);
                  //  if (item != null)
                   //     tool.updateMoney(sale, item, item.StorageInfo.Profit);
                  //  setSettleMsg(i.ToString());
                    sql = session.CreateSQLQuery(string.Format("update  t_sale set settleState={0} where commodityID='{1}' ", sale.settleState,sale.commodityID));
                    sql.ExecuteUpdate();
                    

                   // setSettleMsg(i.ToString());
                    step.Step(i++);
                }
                tool.updateUnSettleData();
                tool.saveall();
                session.Flush();
                //throw new Exception("test");
                 sql = session.CreateSQLQuery("insert into t_hsale select * from t_sale where settleState=1 and  saleType=0");
                sql.ExecuteUpdate();
                step.Over();
                sql = session.CreateSQLQuery("delete from t_sale where settleState=1 and  saleType=0");
                sql.ExecuteUpdate();
               // db.ExeSql("insert into t_hsale select * from t_sale where settleState=1 and  saleType=0");
               // db.ExeSql("delete from t_sale where settleState=1 and  saleType=0");
                step.Over();

           
            return true;
        }


    }
}
